package com.twu.biblioteca;

import com.twu.biblioteca.menuoptions.*;

import java.util.HashMap;

/**
 * Created by olga on 31/05/14.
 */
public class Menu
{
    private static Menu menuInstance;
    private HashMap<Integer, MenuOption> menuOptions;
    private LibraryManager libraryManager;

    private static final String MENU_HEADER = "Please select one of the following actions:";
    private static final String LIBRARY_MANAGER_NOT_NET_ERROR = "Unable to access library manager. Please try again later.";

    public Menu()
    {
        UserDetailsHandler userDetailsHandler = new UserDetailsHandler();

        menuOptions = new HashMap<Integer, MenuOption>();
        menuOptions.put( 1, new ListBooksOption() );
        menuOptions.put( 2, new CheckOutBookOption(userDetailsHandler) );
        menuOptions.put( 3, new ReturnBookOption() );
        menuOptions.put( 4, new ListMoviesOption() );
        menuOptions.put( 5, new CheckOutMovieOption(userDetailsHandler) );
        menuOptions.put( 6, new ReturnMovieOption() );
        menuOptions.put( 7, new QuitMenuOption() );
    }

    public static Menu getInstance()
    {
        if( menuInstance == null )
        {
            menuInstance = new Menu();
        }
        return menuInstance;
    }

    public HashMap<Integer, MenuOption> getMenuOptions() { return menuOptions; }

    public void setLibraryManager(LibraryManager libraryManager)
    {
        this.libraryManager = libraryManager;
    }

    public LibraryManager getLibraryManager()
    {
        return libraryManager;
    }

    public void printMenu()
    {
        TerminalPrintUtility.printStringWithCarriageReturn(MENU_HEADER);
        for( int key : this.menuOptions.keySet() )
            TerminalPrintUtility.printStringWithCarriageReturn("  " + key + ". " + menuOptions.get(key).getMenuTitle() );
        TerminalPrintUtility.printStringWithCarriageReturn("");
    }

    public int parseUserOptionToInt(String userOption)
    {
        try
        {
            return Integer.parseInt(userOption);
        }
        catch(NumberFormatException e)
        {
            return -1;
        }
    }

    public MenuOption getUserOption(String userOption)
    {
        int optionNumber = parseUserOptionToInt(userOption);

        if( optionNumber == -1 || !menuOptions.containsKey(optionNumber))
        {
            TerminalPrintUtility.printInvalidOptionErrorMessage();
            return null;
        }
        else
            return menuOptions.get(optionNumber);
    }

    public boolean processUserAction( String userOption )
    {
        if( libraryManager == null )
        {
            TerminalPrintUtility.printStringWithEmptyLine(LIBRARY_MANAGER_NOT_NET_ERROR);
            return false;
        }

        MenuOption selectedOption = getUserOption(userOption);
        if( selectedOption == null )
            return false;
        if(selectedOption.isQuitOption())
            return true;

        selectedOption.action( libraryManager );
        return false;
    }
}
